<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 19/9/23
 * Time: 17:16
 */

namespace app\store\controller;


use app\common\bean\GlobalBean;
use app\common\bean\ListMap;
use app\common\bean\StoreRoleBean;
use app\common\bean\StoreRolePermissionBean;
use app\common\controller\StoreController;
use app\common\manage\StoreRoleManage;
use app\common\manage\StoreRolePermissionManage;
use app\common\utils\ExitJsonUtil;

class StoreRole extends StoreController
{
    /** 获取角色列表
     * @return mixed
     * @throws \Exception
     */
    public function roleList(){
        $listMap = new ListMap();
        $listMap->setData($this->getParams);
        $roleListMap = StoreRoleManage::getInstance()->getPaginatorList($listMap);
        $this->assign("roleList",$roleListMap->getList());
        $this->assign("currentPage",$roleListMap->getCurrentPage());
        $this->assign("totalCount",$roleListMap->getTotalCount());
        return $this->fetch();
    }

    /**
     * 添加角色
     * @throws \Exception
     */
    public function addRole(){
        if(request()->isPost()){
            $storeRoleBean = new StoreRoleBean();
            $storeRoleBean->setData($this->postParams);
            $result = StoreRoleManage::getInstance()->add($storeRoleBean);
            ExitJsonUtil::getInstance()->exitData($result);
        }
        return $this->fetch();
    }

    /**
     * 修改角色
     * @throws \Exception
     */
    public function editRole(){
        $storeRoleBean = new StoreRoleBean();
        $storeRoleBean->setData($this->getParams);
        if(request()->isPost()){
            $storeRoleBean->setData($this->postParams);
            $result = StoreRoleManage::getInstance()->edit($storeRoleBean);
            ExitJsonUtil::getInstance()->exitData($result);
        }
        $adminRoleBean = StoreRoleManage::getInstance()->getInfo($storeRoleBean);
        $this->assign('adminRole',$adminRoleBean->getData());
        return $this->fetch();
    }

    /**
     * 删除角色
     * @throws \Exception
     */
    public function delRole(){
        $storeRoleBean= new StoreRoleBean();
        $storeRoleBean->setData($this->getParams);
        $result = StoreRoleManage::getInstance()->delete($storeRoleBean);
        ExitJsonUtil::getInstance()->exitData($result);
    }

    /** 分配主后台角色权限
     * @return mixed
     * @throws \Exception
     */
    public function allocationPermissions(){
        $storeRolePermissionBean = new StoreRolePermissionBean();
        $storeRolePermissionBean->setData($this->getParams);
        if(request()->isPost()){
            $permissionIds = $this->postParams['ids'] ?? [];
            $result = StoreRolePermissionManage::getInstance()->allocationPermissions($storeRolePermissionBean,$permissionIds);
            ExitJsonUtil::getInstance()->exitData($result);
        }
        $permissionTree = StoreRolePermissionManage::getInstance()->getStoreRoleHavePermissionTree($storeRolePermissionBean);
        $this->assign('permissionTree',json_encode($permissionTree));
        return $this->fetch();
    }
}